package com.xqb.listen.manager

import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.xqb.listen.R
import com.xqb.listen.manager.media.model.MediaModel
import com.xqb.listen.model.PlayStatus
import com.xqb.listen.service.MusicForegroundService
import com.xqb.listen.ui.MainActivity


/**
 * author:17873
 * date:2025/1/28
 * des:ForegroundServiceUtil
 **/
object ForegroundServiceManager {

    private const val channelId = "001"

    @SuppressLint("ResourceAsColor")
    fun createNotification(context: Context, intent: Intent?) : Notification{
        // 创建通知渠道（仅适用于 Android O 及以上版本）
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManagerCompat.from(context).createNotificationChannel(NotificationChannel(
                channelId , context.packageName, NotificationManager.IMPORTANCE_DEFAULT))
        }

        if(intent == null || intent.action.isNullOrEmpty()){
            MediaPlayManager.singleInstance.currentState.value = PlayStatus.ERROR.name
        }else{
            MediaPlayManager.singleInstance.currentState.value = intent.action!!
        }

//        val smallRemoteView = RemoteViews(context.packageName , R.layout.notify_layout).apply {
//            setImageViewResource(R.id.imgPlayPause , if(isPlay)  R.drawable.ic_play else R.drawable.ic_pause)
//            setOnClickPendingIntent(R.id.imgLast , getNotifyPendingIntent(context , LAST))
//            if(isPlay) {
//                setOnClickPendingIntent(R.id.imgPlayPause , getNotifyPendingIntent(context , PLAY))
//            }else{
//                setOnClickPendingIntent(R.id.imgPlayPause , getNotifyPendingIntent(context , PAUSE))
//            }
//
//            setOnClickPendingIntent(R.id.imgNext , getNotifyPendingIntent(context , NEXT))
//        }


        return NotificationCompat.Builder(context, channelId)
            .setContentTitle(context.getString(R.string.app_name))
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setSmallIcon(R.drawable.ic_app_icon_notify)
            .setContentIntent(getPendingIntent(context))
            .setStyle(NotificationCompat.DecoratedCustomViewStyle())
            .setAutoCancel(false)
            .setShowWhen(false)
            .setOnlyAlertOnce(true)
            .setOngoing(true)
            .setContent(getPlayPauseRemoteView(MediaPlayManager.singleInstance.currentState.value , MediaPlayManager.singleInstance.currentMedia , context))
//            .setCustomBigContentView(bigRemoteView)
            .build()
    }

    private fun getPendingIntent(context: Context): PendingIntent {
        return PendingIntent.getActivity(
            context,
            1,
            Intent(context, MainActivity::class.java),
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
                PendingIntent.FLAG_IMMUTABLE
            else
                PendingIntent.FLAG_UPDATE_CURRENT
        )
    }

    private fun getPlayPauseRemoteView(state : String , currentMedia : MediaModel? , context: Context): RemoteViews {
        return RemoteViews(context.packageName , R.layout.notify_layout_large).apply {
            setOnClickPendingIntent(R.id.imgLast , getNotifyPendingIntent(context , PlayStatus.LAST.name))
            setOnClickPendingIntent(R.id.imgNext , getNotifyPendingIntent(context , PlayStatus.NEXT.name))

            if(currentMedia != null){
                when (state) {
                    PlayStatus.PLAY.name -> {
                        setImageViewResource(R.id.imgPlayPause , R.drawable.ic_play)
                        setOnClickPendingIntent(R.id.imgPlayPause , getNotifyPendingIntent(context , PlayStatus.PAUSE.name))
                        MediaPlayManager.singleInstance.resumeAudio(context , false)
                    }
                    PlayStatus.PAUSE.name -> {
                        setImageViewResource(R.id.imgPlayPause , R.drawable.ic_pause)
                        setOnClickPendingIntent(R.id.imgPlayPause , getNotifyPendingIntent(context , PlayStatus.PLAY.name))
                        MediaPlayManager.singleInstance.pauseAudio(context , false)
                    }
                    else -> {
                        setImageViewResource(R.id.imgPlayPause , R.drawable.ic_pause)
                    }
                }
                setTextViewText(R.id.txMusicName , currentMedia.filename)
                setTextViewText(R.id.txAuthorName ,currentMedia.author)
            }else{
                setImageViewResource(R.id.imgPlayPause , R.drawable.ic_pause)
                setTextViewText(R.id.txMusicName , context.getString(R.string.app_name))
                setTextViewText(R.id.txAuthorName ,"甜瓜旁的长白山")
            }
        }
    }

    private fun getNotifyPendingIntent(context : Context, type : String) : PendingIntent {
        return PendingIntent.getService(
            context,
            2,
            Intent(context , MusicForegroundService::class.java).apply
            {
                action = type
            },
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
                PendingIntent.FLAG_IMMUTABLE
            else
                PendingIntent.FLAG_UPDATE_CURRENT
        )
    }



//    fun updateNotifyState(context: Context , state : String){
//        val intent  = Intent(context, MusicForegroundService::class.java)
//        intent.putExtra(Constants.AUDIO , currentMedia)
//        intent.action = state
//        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
//            context.startForegroundService(intent)
//        }else{
//            context.startService(intent)
//        }
//    }
}